package com.dongpu.springboot.rabbitmq.original;

import com.dongpu.springboot.rabbitmq.utils.ConnectionUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeoutException;

/**
 * @author biyuyang
 *
 * Java原生类支持RabbitMQ,直接运行主方法即可
 *
 */
public class ConfirmReceiver1 {

	private final static String QUEUE_NAME = "confirm";

	public static void main(String[] args) throws IOException, TimeoutException {
		// 获取到连接以及mq通道
		Connection connection = ConnectionUtil.getConnection();
		Channel channel = connection.createChannel();
		channel.queueDeclare(QUEUE_NAME,false,false,false,null);
		System.out.println("ConfirmReceiver1 waiting for messages. To exit press CTRL+C");

		// 创建队列消费者
		final Consumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
				byte[] body) {
				SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");

				String message = new String(body, StandardCharsets.UTF_8);

				System.out.println(" ConfirmReceiver1  : " + message);
				System.out.println(" ConfirmReceiver1 Done! at " + time.format(new Date()));
			}
		};
		channel.basicConsume(QUEUE_NAME, true, consumer);
	}
}
